﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyEmployees.BLL;

namespace MyEmployees
{
    public partial class FrmEmployees : Form
    {
        EmployeeManage empManage;
        
        public FrmEmployees()
        {
            InitializeComponent();
        }

        private void FrmEmployees_Load(object sender, EventArgs e)
        {
            empManage = new EmployeeManage();
            dataGridView1.DataSource = empManage.GetEmployeesList();

        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count < 1)
            {
                MessageBox.Show("请选中一行后再进行删除操作！");
                return;
            }
            int empID = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);
            int result = empManage.DeleteEmployee(empID);
            if (result == 1)
            {
                MessageBox.Show("删除成功！");
                dataGridView1.DataSource = empManage.GetEmployeesList();
            }
            else if (result == -1)
                MessageBox.Show("该雇员还存在订单信息，不能删除！");
            else
                MessageBox.Show("删除失败！");
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmAdd fmNew = new FrmAdd();	
            fmNew.ShowDialog();
            dataGridView1.DataSource = empManage.GetEmployeesList();
        }

        private void updateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count <= 0)		//用于判断是否选中了DataGridView中的一行
            {
                MessageBox.Show("请选中一行进行操作");
                return;
            }
            int empID = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);
            FrmUpdate fmEdit = new FrmUpdate(empID);	//将EmployeeID传入Form3中
            fmEdit.ShowDialog();
            dataGridView1.DataSource = empManage.GetEmployeesList();
        }
    }
}
